```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java动态代理机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8f9fa;
            color: #1a1a1a;
        }
        .hero {
            background: linear-gradient(135deg, #6e48aa 0%, #9d50bb 100%);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.8em;
            margin-right: 0.1em;
            color: #6e48aa;
            font-weight: 700;
        }
        .mermaid-container {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .code-block {
            background-color: #2d2d2d;
            color: #f8f8f2;
            border-radius: 0.5rem;
            padding: 1rem;
            font-family: 'Courier New', Courier, monospace;
            overflow-x: auto;
        }
        .keyword {
            color: #f92672;
        }
        .type {
            color: #66d9ef;
        }
        .string {
            color: #e6db74;
        }
        .comment {
            color: #75715e;
        }
        .footer {
            background-color: #1a1a1a;
            color: #f8f9fa;
        }
        .footer a:hover {
            color: #9d50bb;
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0 md:pr-10">
                    <h1 class="text-4xl md:text-5xl font-bold mb-4 font-serif">Java动态代理机制</h1>
                    <h2 class="text-2xl md:text-3xl font-medium mb-6">接口与实现的完美解耦</h2>
                    <p class="text-lg opacity-90 mb-8">探索JDK动态代理的工作原理、限制以及替代方案，深入理解Java反射机制的强大之处。</p>
                    <div class="flex space-x-4">
                        <a href="#jdk-proxy" class="bg-white text-purple-700 px-6 py-2 rounded-full font-medium hover:bg-purple-100 transition duration-300">JDK代理</a>
                        <a href="#cglib-proxy" class="bg-transparent border-2 border-white px-6 py-2 rounded-full font-medium hover:bg-white hover:text-purple-700 transition duration-300">CGLIB代理</a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="mermaid-container">
                        <div class="mermaid">
                            graph TD
                            A[客户端] --> B[代理对象]
                            B --> C[InvocationHandler]
                            C --> D[实际对象]
                            style A fill:#6e48aa,color:white
                            style B fill:#9d50bb,color:white
                            style C fill:#c792ea,color:white
                            style D fill:#7e57c2,color:white
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl py-12 px-4 md:px-0">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl p-8 shadow-md">
                <p class="drop-cap text-lg leading-relaxed">
                    JDK 动态代理是Java编程中一种强大的设计模式实现方式，它通过运行时动态生成代理类来实现对目标对象的间接访问。这种机制在设计上有一个显著特点：只能对实现了接口的类生成代理。这源于它依赖于Java的接口机制和反射来创建代理类，并转发方法调用。代理对象通过实现接口的方法调用将请求转发给<code>InvocationHandler</code>。对于未实现接口的类，开发人员可以使用CGLIB等库来实现代理。这样，Java生态系统实际上提供了两种代理机制，分别处理不同的代理需求。
                </p>
            </div>
        </section>

        <!-- JDK Proxy Section -->
        <section id="jdk-proxy" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center text-gray-800 font-serif">
                <i class="fas fa-cogs mr-3 text-purple-600"></i> JDK动态代理的工作机制
            </h2>

            <div class="grid md:grid-cols-2 gap-8">
                <div class="card bg-white rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">核心组件</h3>
                    <p class="text-gray-700 mb-4">
                        JDK动态代理依赖于<code>java.lang.reflect.Proxy</code>类及其相关的<code>InvocationHandler</code>接口来生成代理对象。其核心机制如下：
                    </p>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-cube text-purple-500 mt-1 mr-2"></i>
                            <span><code>Proxy</code><strong>类：</strong>提供了静态方法<code>newProxyInstance</code>，用于创建代理对象。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-exchange-alt text-purple-500 mt-1 mr-2"></i>
                            <span><code>InvocationHandler</code><strong>接口：</strong>代理对象的所有方法调用都会被转发到实现了<code>InvocationHandler</code>接口的对象上，后者负责处理这些方法调用。</span>
                        </li>
                    </ul>
                </div>

                <div class="card bg-white rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">创建过程</h3>
                    <ol class="list-decimal pl-5 space-y-3 text-gray-700">
                        <li><strong>创建代理类：</strong><code>Proxy.newProxyInstance</code>方法创建一个代理类。代理类是在运行时动态生成的，其实现了指定的接口。</li>
                        <li><strong>方法调用转发：</strong>所有通过代理对象调用的方法都会被<code>InvocationHandler</code>的<code>invoke</code>方法处理。</li>
                    </ol>
                </div>
            </div>

            <div class="mt-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">示例代码</h3>
                <div class="code-block">
                    <pre><span class="keyword">public class</span> <span class="type">ProxyExample</span> {
    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="type">main</span>(<span class="type">String</span>[] args) {
        <span class="comment">// 创建实际对象</span>
        <span class="type">RealSubject</span> real = <span class="keyword">new</span> <span class="type">RealSubject</span>();
        
        <span class="comment">// 创建代理对象</span>
        <span class="type">Subject</span> proxy = (<span class="type">Subject</span>) <span class="type">Proxy</span>.newProxyInstance(
            <span class="type">Subject</span>.<span class="keyword">class</span>.getClassLoader(),
            <span class="keyword">new</span> <span class="type">Class</span>[]{<span class="type">Subject</span>.<span class="keyword">class</span>},
            <span class="keyword">new</span> <span class="type">InvocationHandler</span>() {
                <span class="keyword">public</span> <span class="type">Object</span> <span class="type">invoke</span>(<span class="type">Object</span> proxy, <span class="type">Method</span> method, <span class="type">Object</span>[] args) 
                    <span class="keyword">throws</span> <span class="type">Throwable</span> {
                    <span class="comment">// 前置处理</span>
                    <span class="type">System</span>.out.println(<span class="string">"Before method call"</span>);
                    
                    <span class="comment">// 调用实际对象的方法</span>
                    <span class="type">Object</span> result = method.invoke(real, args);
                    
                    <span class="comment">// 后置处理</span>
                    <span class="type">System</span>.out.println(<span class="string">"After method call"</span>);
                    <span class="keyword">return</span> result;
                }
            }
        );
        
        <span class="comment">// 通过代理调用方法</span>
        proxy.request();
    }
}</pre>
                </div>
            </div>
        </section>

        <!-- Limitations Section -->
        <section id="limitations" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center text-gray-800 font-serif">
                <i class="fas fa-exclamation-triangle mr-3 text-yellow-500"></i> 为什么只能对实现了接口的类生成代理
            </h2>

            <div class="grid md:grid-cols-2 gap-8">
                <div class="card bg-white rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">设计原理</h3>
                    <p class="text-gray-700 mb-4">
                        JDK动态代理的设计依赖于Java的接口机制，而不是类的继承。这种选择有其深层次的技术原因：
                    </p>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-project-diagram text-purple-500 mt-1 mr-2"></i>
                            <span><strong>接口基于类型：</strong>JDK动态代理是通过实现接口来完成的。生成的代理类需要实现一个或多个接口，这样代理对象就可以通过这些接口进行方法调用。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-lock text-purple-500 mt-1 mr-2"></i>
                            <span><strong>动态代理的限制：</strong>JDK动态代理机制本质上是通过<code>Proxy</code>类创建一个新的类，这个新类实现了提供的接口。因此，代理对象只能代理接口中的方法。</span>
                        </li>
                    </ul>
                </div>

                <div class="card bg-white rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">技术限制</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-ban text-purple-500 mt-1 mr-2"></i>
                            <span><strong>无法处理类实现：</strong>动态代理无法处理类的具体实现，因为代理对象无法继承或修改类的状态。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-code-branch text-purple-500 mt-1 mr-2"></i>
                            <span><strong>接口方法的公开性：</strong>由于接口方法是公开的，代理机制可以在运行时正确处理方法调用，而类的方法可能包含私有或受保护的访问限制。</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- CGLIB Section -->
        <section id="cglib-proxy" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center text-gray-800 font-serif">
                <i class="fas fa-code mr-3 text-green-500"></i> 如何处理未实现接口的类
            </h2>

            <div class="grid md:grid-cols-2 gap-8">
                <div class="card bg-white rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">CGLIB代理</h3>
                    <p class="text-gray-700 mb-4">
                        对于没有实现接口的类，Java生态系统中提供了另一种代理机制：<strong>CGLIB（Code Generation Library）</strong>。
                    </p>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-layer-group text-purple-500 mt-1 mr-2"></i>
                            <span><strong>基于继承：</strong>CGLIB是一个强大的字节码生成库，能够在运行时生成类的子类，并通过继承的方式实现代理。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-tachometer-alt text-purple-500 mt-1 mr-2"></i>
                            <span><strong>性能考量：</strong>CGLIB能够代理没有实现接口的类，但由于需要生成代理类的子类，在某些情况下可能导致性能问题。</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-exclamation-circle text-purple-500 mt-1 mr-2"></i>
                            <span><strong>限制：</strong>CGLIB与最终类（<code>final</code>类）不兼容，因为无法生成最终类的子类。</span>
                        </li>
                    </ul>
                </div>

                <div class="card bg-white rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-purple-700">Spring框架中的应用</h3>
                    <p class="text-gray-700 mb-4">
                        在Spring框架中，Spring AOP智能地结合了两种代理机制：
                    </p>
                    <div class="mermaid-container">
                        <div class="mermaid">
                            graph LR
                            A[目标对象] --> B{实现接口?}
                            B -->|是| C[JDK动态代理]
                            B -->|否| D[CGLIB代理]
                            style A fill:#7e57c2,color:white
                            style B fill:#5c6bc0,color:white
                            style C fill:#26a69a,color:white
                            style D fill:#26c6da,color:white
                        </div>
                    </div>
                    <p class="text-gray-700 mt-4">
                        Spring AOP会根据目标对象是否实现接口来自动选择代理机制。如果目标对象实现了接口，则使用JDK动态代理；否则，使用CGLIB代理。
                    </p>
                </div>
            </div>

            <div class="mt-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">CGLIB示例代码</h3>
                <div class="code-block">
                    <pre><span class="keyword">public class</span> <span class="type">CglibExample</span> {
    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="type">main</span>(<span class="type">String</span>[] args) {
        <span class="type">Enhancer</span> enhancer = <span class="keyword">new</span> <span class="type">Enhancer</span>();
        enhancer.setSuperclass(<span class="type">RealSubject</span>.<span class="keyword">class</span>);
        enhancer.setCallback(<span class="keyword">new</span> <span class="type">MethodInterceptor</span>() {
            <span class="keyword">public</span> <span class="type">Object</span> <span class="type">intercept</span>(<span class="type">Object</span> obj, <span class="type">Method</span> method, 
                <span class="type">Object</span>[] args, <span class="type">MethodProxy</span> proxy) <span class="keyword">throws</span> <span class="type">Throwable</span> {
                <span class="type">System</span>.out.println(<span class="string">"Before method call"</span>);
                <span class="type">Object</span> result = proxy.invokeSuper(obj, args);
                <span class="type">System</span>.out.println(<span class="string">"After method call"</span>);
                <span class="keyword">return</span> result;
            }
        });
        
        <span class="type">RealSubject</span> proxy = (<span class="type">RealSubject</span>) enhancer.create();
        proxy.request();
    }
}</pre>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center text-gray-800 font-serif">
                <i class="fas fa-balance-scale mr-3 text-blue-500"></i> JDK代理与CGLIB代理对比
            </h2>

            <div class="grid md:grid-cols-2 gap-8">
                <div class="card bg-white rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-center text-purple-700">JDK动态代理</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>基于接口实现</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>Java标准库内置，无需额外依赖</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>性能较好，适用于简单代理场景</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                            <span>只能代理接口方法</span>
                        </li>
                    </ul>
                </div>

                <div class="card bg-white rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-4 text-center text-purple-700">CGLIB代理</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>基于继承实现</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>可以代理没有接口的类</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>功能更强大，支持方法拦截</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                            <span>需要额外依赖库</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                            <span>无法代理final类和方法</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times-circle text-red-500 mt-1 mr-2"></i>
                            <span>性能略低于JDK代理</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="footer py-8">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-medium">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="hover:text-purple-300 transition duration-200">http://www.yuque.com/jtostring</a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```